package com.gxa.controller.community;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.communitydto.HouseNameDto;
import com.gxa.dto.communitydto.OwnerDto;
import com.gxa.dto.communitydto.OwnerHouseDto;
import com.gxa.dto.communitydto.OwnerSearchDto;
import com.gxa.entity.community.Community;
import com.gxa.entity.community.House;
import com.gxa.entity.community.Owner;
import com.gxa.service.community.OwnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Api(tags = "业主列表接口")
@RestController
@RequestMapping("/owner")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "业主列表接口")
    @GetMapping("/list")
    public Result<PageInfo<OwnerDto>> queryAll(OwnerSearchDto ownerSearchDto ,Integer pageNum,Integer pageSize){
        PageInfo<OwnerDto> ownerDtoPageInfo;
        try {
            ownerDtoPageInfo = ownerService.queryAll(ownerSearchDto, pageNum, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败。");
        }
        return Result.success(ownerDtoPageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "新增业主接口")
    @PostMapping("/add")
    public Result add(Owner owner){
        try {
            ownerService.add(owner);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败。");
        }
       /* try {
            ownerService.addOhRelation(owner);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败。");
        }*/
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "删除业主接口")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        try {
            ownerService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("删除失败。");
        }
        return Result.success();
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "根据id查询业主接口")
    @GetMapping("/queryById/{id}")
    public Result<OwnerDto> queryById(@PathVariable("id") Integer id){
        OwnerDto ownerDto;
        try {
            ownerDto = ownerService.queryById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败。");
        }
        return Result.success(ownerDto);
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "编辑业主接口")
    @PutMapping("/edit")
    public Result edit(@RequestBody Owner owner){
        try {
            ownerService.update(owner);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("编辑失败。");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "根据用户编码查询所有的房产接口")
    @GetMapping("/queryByOwnerNum/{ownerNum}")
    public Result queryByOwnerNum(@PathVariable("ownerNum") String ownerNum){
        List<OwnerHouseDto> ownerHouseDtos;
        try {
            ownerHouseDtos = ownerService.queryByOwnerNum(ownerNum);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败。");
        }
        return Result.success(ownerHouseDtos);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "查询所有房屋的接口")
    @GetMapping("/getHouseNames")
    public Result getHouseName(){
        List<HouseNameDto> houseNameDtos;
        try {
            houseNameDtos = ownerService.queryHouseName();
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败。");
        }
        return Result.success(houseNameDtos);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "异步请求判断业主编号是否重复的接口")
    @GetMapping("/judgeOne")
    public Result judgeOne(String ownerNum){
        List<Owner> owners = ownerService.queryByNum(ownerNum);
        if (owners.size() != 0) {
            return Result.failed("业主编码重复了，请重新输入");
        } else {
            return Result.success();
        }

    }
}
